#include "handoff.h"
#include "ui_handoff.h"
#include "globalval.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "mqtt/qmqtt.h"

handoff::handoff(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::handoff)
{
    ui->setupUi(this);
    temp1 = 0;
    temp2 = 1;
    temp3 = 1;
    label = new QLabel(this);
    label->setGeometry(20,420,200,50);

//    label->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    label->setStyleSheet("background-color: rgba(255, 255, 255, 0);color: black;font-family: '黑体'; font-size: 18px;");
    ui->pushButton->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
    ui->pushButton_2->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
    ui->pushButton_3->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(ui->pushButton);
    layout->addWidget(ui->pushButton_2);
    layout->addWidget(ui->pushButton_3);
    layout->addWidget(label);
    setLayout(layout);
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout,this,&handoff::updateTime);
    timer->start(1000);
    updateTime();
}

handoff::~handoff()
{
    delete ui;
}

void handoff::on_pushButton_clicked()
{
    if(temp1==1)
    {
        ui->pushButton->setText("循环模式|关");
        ui->pushButton->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp1 = 0;
        if(temp2 != 1)
        {
            handoff::on_pushButton_2_clicked();

        }
        if(temp3 !=1)
        {
            handoff::on_pushButton_3_clicked();
        }
        QJsonObject object;
        object["mode"] = 1;
        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
    else
    {
        ui->pushButton->setText("循环模式|开");
        ui->pushButton->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp1 = 1;
    }
}

void handoff::on_pushButton_2_clicked()
{
    if(temp2==1)
    {
        ui->pushButton_2->setText("对比模式|关");
        ui->pushButton_2->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp2 = 0;
        if(temp1!=1)
        {
            handoff::on_pushButton_clicked();
        }
        if(temp3!=1)
        {
            handoff::on_pushButton_3_clicked();
        }
        QJsonObject object;
        object["mode"] = 2;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
    else
    {
        ui->pushButton_2->setText("对比模式|开");
        ui->pushButton_2->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp2 = 1;

    }
}

void handoff::on_pushButton_3_clicked()
{
    if(temp3==1)
    {
        ui->pushButton_3->setText("刷新模式|关");
        ui->pushButton_3->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp3 = 0;
        if(temp1!=1)
        {
            handoff::on_pushButton_clicked();
        }
        if(temp2!=1)
        {
            handoff::on_pushButton_2_clicked();
        }
        QJsonObject object;
        object["mode"] = 3;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
    else
    {
        ui->pushButton_3->setText("刷新模式|开");
        ui->pushButton_3->setStyleSheet("background-color: rgb(185, 255, 185);color: black;font-family: '黑体'; font-size: 28px;");
        temp3 = 1;

    }
}

void handoff::updateTime()
{
    label->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
